## Milena Ang & Monika Nalepa
## April 2019
## Replication file for tables and figures of
## Appendix C: Data Creation and Summary Statistics
## Appendix D: Robustness Checks
## "Can Transitional Justice Improve the Quality of Representation in New Democracies?"

# Libraries
library(dplyr)
library(tidyr)
library(foreign)
library(lme4)
library(stargazer, quietly = TRUE)
library(xtable)
options(xtable.comment = FALSE)
library(stringr)
library(plotrix)
library(panelView)

# Data:
# Set working directory
data <- read.csv("data.csv",
                 stringsAsFactors = FALSE)

####################################################################
## Figure b: Parties by country included in the analysis

tab <- table(data$ccodewb)
tab <- sort(tab)

pdf("figureb.pdf",
    family="Times", pointsize=5)
par(mar=c(1,1.2,0.5,0.2),
    oma=c(3,1,0,0),
    mgp=c(0.4,0.2,0.2))

barplot(tab, ylim =  c(0, 18),
        yaxt = "n",
        cex.names = 0.75,
        names.arg = names(tab),
        las = 2)
axis(2, pos = 0, cex = 0.8,
     at = c(0,2,4,6,8,10,12,14,16,18), labels = c("0 ","2 ","4 ","6 ","8 ","10","12","14","16","18"),
     las = 1)
dev.off()
remove(tab)


####################################################################
## Table a: Summary statistics for party variables
sf <- function (x){
  if( is.numeric(x)!=TRUE) {stop("Supplied X is not numeric")}
  mysummary = data.frame(
    "Min." =as.numeric(min(x, na.rm = TRUE)),
    "1st Qu." = quantile(x, na.rm = TRUE)[2],
    "Median" = median(x, na.rm = TRUE),
    "Mean" = mean(x, na.rm = TRUE),
    "3rd Qu." = quantile(x, na.rm = TRUE)[4],
    "Max." = max(x, na.rm = TRUE),
    "N" = length(x),
    row.names=""
  )
  names(mysummary) = c("Min.","1st Qu.","Median",
                       "Mean","3rd Qu.","Max.",
                       "$N$")
  return( mysummary )
}

rbind(sf(data$cosal_3),
             sf(data$d_party_3),
             sf(data$d1),
             sf(data$d2),
             sf(data$d3),
             sf(data$d4),
             sf(data$d5))

####################################################################
## Table b: Correlation between party-level variables
party_vars <- cbind(data$cosal_3,
                    data$d_party_3,
                    data$d1,
                    data$d2,
                    data$d3,
                    data$d4,
                    data$d5)

cor(party_vars, use = "complete.obs")
remove(party_vars)
####################################################################
## Table c: Summary statistics for country-level variables

country_data <- data %>%
  group_by(country, ccodewb) %>%
  summarise(tyear = mean(tyear),
            nyear = mean(nyear),
            pressfreedom = mean(pressfreedom),
            severity = mean(severity),
            lparty = mean(lparty),
            gdppc2008 = mean(gdppc2008),
            gdppc_trans = mean(gdppc_trans),
            miss_succ = mean(miss_succ),
            margin_1 = mean(margin_1),
            miss_ele = mean(miss_ele))

rbind(sf(country_data$severity),
      sf(country_data$nyear),
      sf(country_data$pressfreedom),
      sf(country_data$lparty),
      sf(country_data$miss_succ))

####################################################################
## Table d: Correlation between country-level variables
country_vars <- cbind(country_data$severity,
                      country_data$nyear,
                      country_data$pressfreedom,
                      country_data$lparty,
                      country_data$miss_succ)
cor(country_vars, use = "complete.obs")

remove(country_vars)

####################################################################
## Table e: Quality of representation and predicted severity of lustration
country_data <- country_data %>%
  mutate(loggdppc_trans = log(gdppc_trans)) %>%
  filter(ccodewb != "NIR") # Ireland will not be used

# We estimate this using an ols in which the level of severity is a function
# of successor, gdppc and number of years observed
pred_model <- lm(severity ~ loggdppc_trans +
                   nyear +
                   margin_1 +
                   miss_ele, 
                 data = country_data)

# We then predict the value of severity and
# use that instead of observed severity.
country_data$pred <- predict(pred_model)
data <- data %>%
  left_join(country_data %>%
              select(country,pred))

# And we estimate the original models using the predicted values:
m_1 <- lmer(cosal_3 ~ d_party_3 + 
              miss_succ +
              (1 | country), 
            data = data)
m_2 <- lmer(cosal_3 ~ d_party_3 + 
              miss_succ + 
              pred +
              (1 | country),
            data = data)
m_3 <- lmer(cosal_3 ~ d_party_3 +
              miss_succ +
              pred +
              nyear +
              (1 | country),
            data = data)
m_4 <- lmer(cosal_3 ~ d_party_3 +
              miss_succ + 
              pred +
              nyear +
              pressfreedom +
              (1 | country),
            data = data)
m_5 <- lmer(cosal_3 ~ d_party_3 +
              pred +
              nyear +
              pressfreedom +
              lparty + 
              (1 | country),
            data = data)

tab_models <- stargazer(m_1, m_2, m_3, m_4, m_5,
                        header = FALSE, type = "html",
                        out = "tablee.html",
                        title = "Quality of representation and predicted severity of lustration",
                        omit = c("miss_succ"),
                        dep.var.caption="",
                        dep.var.labels = c("",""),
                        covariate.labels = c("party distance",
                                             "predicted severity",
                                             "years since transition",
                                             "press freedom",
                                             "opposition status"),
                        add.lines = list(c("Country intercepts", "Y", "Y", "Y", "Y", "Y")),
                        column.labels = c("Model 1", "Model 2",
                                          "Model 3", "Model 4",
                                          "Model 5"),
                        model.numbers = FALSE)

remove(m_1, m_2, m_3, m_4, m_5,
       pred_model, tab_models)
####################################################################
## Table f: Quality of representation (controlling for log(GDP per capita))
data <- data %>%
  mutate(loggdppc = log(gdppc_trans))

m_1 <- lmer(cosal_3 ~ d_party_3 +
              miss_succ +
              loggdppc +
              (1 | country), 
            data = data)
m_2 <- lmer(cosal_3 ~ d_party_3 +
              miss_succ +
              loggdppc +
              severity +
              (1 | country),
            data = data)
m_3 <- lmer(cosal_3 ~ d_party_3 + 
              miss_succ +
              loggdppc +
              severity + 
              nyear +
              (1 | country),
            data = data)

m_4 <- lmer(cosal_3 ~ d_party_3 +
              miss_succ +
              loggdppc +
              severity + 
              nyear +
              pressfreedom +
              (1 | country),
            data = data)

m_5 <- lmer(cosal_3 ~ d_party_3 +
              miss_succ +
              loggdppc +
              severity +
              nyear +
              pressfreedom +
              lparty + 
              (1 | country),
            data = data)

tab_models <- stargazer(m_1, m_2, m_3, m_4, m_5,
                        header = FALSE, type = "html",
                        out = "tablef.html",
                        title = "Quality of representation (controlling for log(GDP per capita))",
                        dep.var.caption="",
                        dep.var.labels = c("",""),
                        omit = c("miss_succ"),
                        covariate.labels = c("party distance",
                                             "log(gdp per capita)",
                                             "severity",
                                             "years since transition",
                                             "press freedom",
                                             "opposition status"),
                        add.lines = list(c("Country intercepts", "Y", "Y", "Y", "Y", "Y"),
                                         c("Missing successor", "Y", "Y", "Y", "Y", "Y")),
                        column.labels = c("Model 1", "Model 2",
                                          "Model 3", "Model 4",
                                          "Model 5"),
                        model.numbers = FALSE)
remove(m_1, m_2, m_3, m_4, m_5, tab_models)
####################################################################
## Table g: Quality of representation (countries before 2008)
data_pre <- read.csv("tj_data_panel.csv",
                    stringsAsFactors = FALSE)  %>%
  filter(year < 2008) %>%
  ## Calculating a new severity measure
  group_by(country) %>%
  summarise(totpostrans_pre = sum(postrans),
            totnegtrans_pre = sum(negtrans),
            nyear_pre = max(yaftr, na.rm = TRUE)) %>%
  mutate(severity_pre = totpostrans_pre/(totpostrans_pre+totnegtrans_pre+1)) %>%
  select(country,severity_pre, totpostrans_pre, totnegtrans_pre, nyear_pre) %>%
  right_join(data)

m_1 <- lmer(cosal_3 ~ d_party_3 +
              miss_succ +
              (1 | country), 
            data = data_pre)

m_2 <- lmer(cosal_3 ~ d_party_3 +
              miss_succ + 
              severity_pre +
              (1 | country),
            data = data_pre)
m_3 <- lmer(cosal_3 ~ d_party_3 +
              miss_succ +
              severity_pre + 
              nyear_pre +
              (1 | country),
            data = data_pre)

m_4 <- lmer(cosal_3 ~ d_party_3 +
              miss_succ +
              severity_pre + 
              nyear_pre +
              pressfreedom +
              (1 | country),
            data = data_pre)

m_5 <- lmer(cosal_3 ~ d_party_3 +
              miss_succ +
              severity_pre +
              nyear_pre +
              pressfreedom +
              lparty + 
              (1 | country),
            data = data_pre)

tab_models <- stargazer(m_1, m_2, m_3, m_4, m_5,
                        header = FALSE, type = "html",
                        out = "tableg.html",
                        title = "Quality of representation (countries before 2008)",
                        dep.var.caption="",
                        dep.var.labels = c("",""),
                        omit = c("miss_succ"),
                        covariate.labels = c("party distance",
                                             "severity (pre-2008)",
                                             "years since transition",
                                             "press freedom",
                                             "opposition status"),
                        add.lines = list(c("Country intercepts", "Y", "Y", "Y", "Y", "Y"),
                                         c("Missing successor", "Y", "Y", "Y", "Y", "Y")),
                        column.labels = c("Model 1", "Model 2",
                                          "Model 3", "Model 4",
                                          "Model 5"),
                        model.numbers = FALSE)
remove(m_1, m_2, m_3, m_4, m_5,data_pre)
####################################################################
## Table h: Quality of representation (years since transition logged)
m_1 <- lmer(cosal_3 ~ d_party_3 +
              miss_succ +
              (1 | country), 
            data = data)
m_2 <- lmer(cosal_3 ~ d_party_3 +
              miss_succ +
              severity +
              (1 | country),
            data = data)
m_3 <- lmer(cosal_3 ~ d_party_3 +
              miss_succ +
              severity + 
              log(nyear) +
              (1 | country),
            data = data)
m_4 <- lmer(cosal_3 ~ d_party_3 +
              miss_succ +
              severity + 
              log(nyear) +
              pressfreedom +
              (1 | country),
            data = data)
m_5 <- lmer(cosal_3 ~ d_party_3 +
              miss_succ + 
              severity +
              log(nyear) +
              pressfreedom +
              lparty + 
              (1 | country),
            data = data)

m_6 <- lmer(cosal_3 ~ d_party_3 +
              miss_succ + 
              severity +
              log(nyear) +
              pressfreedom +
              lparty +
              log(nyear)* severity +
              (1 | country),
            data = data)

tab_models <- stargazer(m_1, m_2, m_3, m_4, m_5, m_6,
                        header = FALSE, type = "html",
                        out = "tableh.html",
                        title = "Quality of representation (years since transition logged)",
                        dep.var.caption="",
                        dep.var.labels = c("",""),
                        omit = c("miss_succ"),
                        covariate.labels = c("party distance",
                                             "severity",
                                             "log(years since transition)",
                                             "press freedom",
                                             "opposition status",
                                             "severity*log(years since transition)"),
                        add.lines = list(c("Country intercepts", "Y", "Y", "Y", "Y", "Y", "Y"),
                                         c("Missing successor", "Y", "Y", "Y", "Y", "Y", "Y")),
                        column.labels = c("Model 1", "Model 2",
                                          "Model 3", "Model 4",
                                          "Model 5", "Model 6"),
                        model.numbers = FALSE)
remove(m_1, m_2, m_3, m_4, m_5,tab_models)
####################################################################
## Table i: Quality of representation (measured as DALP’s cosalpo 4)
m_1 <- lmer(cosalpo_4 ~ 
              severity +
              (1 | country),
            data = data)
m_2 <- lmer(cosalpo_4 ~ 
              severity + 
              nyear +
              (1 | country),
            data = data)
m_3 <- lmer(cosalpo_4 ~ 
              severity + 
              nyear +
              pressfreedom +
              (1 | country),
            data = data)
m_4 <- lmer(cosalpo_4 ~  + 
              severity +
              nyear +
              pressfreedom +
              lparty + 
              (1 | country),
            data = data)

tab_models <- stargazer(m_1, m_2, m_3, m_4,
                        header = FALSE,type = "html",
                        out = "tablei.html",
                        title = "Quality of representation (measured as DALP's cosalpo\\_4)",
                        dep.var.caption="",
                        dep.var.labels = c("",""),
                        omit = c("miss_succ"),
                        covariate.labels = c("severity",
                                             "years since transition",
                                             "press freedom",
                                             "opposition status"),
                        add.lines = list(c("Country intercepts", "Y", "Y", "Y", "Y")),
                        column.labels = c("Model 1", "Model 2",
                                          "Model 3", "Model 4"),
                        model.numbers = FALSE)
remove(m_1, m_2, m_3, m_4, m_5,tab_models)
####################################################################
## Table j: Quality of representation (vote share of successor and margin of victory)

m_1 <- lmer(cosal_3 ~ d_party_3 + miss_succ +
              (1 | country), 
            data = data)
m_2 <- lmer(cosal_3 ~ d_party_3 + miss_succ +
              severity +
              (1 | country),
            data = data)
m_3 <- lmer(cosal_3 ~ d_party_3 + miss_succ +
              severity + 
              nyear +
              (1 | country),
            data = data)
m_4 <- lmer(cosal_3 ~ d_party_3 + miss_succ +
              severity + 
              nyear +
              pressfreedom +
              (1 | country),
            data = data)
m_5 <- lmer(cosal_3 ~ d_party_3 + miss_succ + 
              severity +
              nyear +
              pressfreedom +
              succ_vs_1 + 
              (1 | country),
            data = data)

m_6 <- lmer(cosal_3 ~ d_party_3 + miss_succ + 
              severity +
              nyear +
              pressfreedom +
              margin_1 + 
              (1 | country),
            data = data)

tab_models <- stargazer(m_1, m_2, m_3, m_4, m_5, m_6,
                        header = FALSE, type = "html",
                        out = "tablej.html",
                        title = "Quality of representation (vote share of successor and margin of victory)",
                        dep.var.caption="",
                        dep.var.labels = c("",""),
                        omit = c("miss_succ"),
                        covariate.labels = c("party distance",
                                             "severity",
                                             "years since transition",
                                             "press freedom",
                                             "successor vote share",
                                             "margin of victory"),
                        add.lines = list(c("Country intercepts", "Y", "Y", "Y", "Y", "Y", "Y"),
                                         c("Missing successor", "Y", "Y", "Y", "Y", "Y", "Y")),
                        column.labels = c("Model 1", "Model 2",
                                          "Model 3", "Model 4",
                                          "Model 5", "Model 6"),
                        model.numbers = FALSE)
remove(m_1, m_2, m_3, m_4, m_5,tab_models)
####################################################################
## Table k: Quality of representation, only countries with successor party
m_1 <- lmer(cosal_3 ~ d_party +
              (1 | country), 
            data = data)
m_2 <- lmer(cosal_3 ~ d_party +
              severity +
              (1 | country),
            data = data)
m_3 <- lmer(cosal_3 ~ d_party +
              severity + 
              nyear +
              (1 | country),
            data = data)
m_4 <- lmer(cosal_3 ~ d_party +
              severity + 
              nyear +
              pressfreedom +
              (1 | country),
            data = data)
m_5 <- lmer(cosal_3 ~ d_party +
              severity +
              nyear +
              pressfreedom +
              lparty + 
              (1 | country),
            data = data)

tab_models <- stargazer(m_1, m_2, m_3, m_4, m_5,
                        header = FALSE, type = "html",
                        out = "tablek.html",
                        title = "Quality of representation, only countries with successor party",
                        dep.var.caption="",
                        dep.var.labels = c("",""),
                        covariate.labels = c("party distance",
                                             "severity",
                                             "years since transition",
                                             "press freedom",
                                             "opposition status"),
                        add.lines = list(c("Country intercepts", "Y", "Y", "Y", "Y", "Y")),
                        column.labels = c("Model 1", "Model 2",
                                          "Model 3", "Model 4",
                                          "Model 5"),
                        model.numbers = FALSE)
remove(m_1, m_2, m_3, m_4, m_5,tab_models)
####################################################################
## Table l: Quality of representation (party distance normalized by country)

data_c <- data %>%
  group_by(country) %>%
  summarise(min = min(d_party, na.rm = TRUE),
            max = max(d_party, na.rm = TRUE))
data <- data %>%
  left_join(data_c) %>%
  mutate(d_party_norm = (d_party - min) / (max - min))


m_1 <- lmer(cosal_3 ~ d_party_norm + miss_succ +
              (1 | country), 
            data = data)
m_2 <- lmer(cosal_3 ~ d_party_norm + miss_succ + 
              severity +
              (1 | country),
            data= data)
m_3 <- lmer(cosal_3 ~ d_party_norm + miss_succ + 
              severity + 
              nyear +
              (1 | country),
            data= data)
m_4 <- lmer(cosal_3 ~ d_party_norm + miss_succ +
              severity + 
              nyear +
              pressfreedom +
              (1 | country),
            data= data)
m_5 <- lmer(cosal_3 ~ d_party_norm + miss_succ +
              severity +
              nyear +
              pressfreedom +
              lparty +
              (1 | country),
            data= data)

tab_models<- stargazer(m_1, m_2, m_3, m_4, m_5,
                       header = FALSE, type = "html",
                       out = "tablel.html",
                       title = "Quality of representation (party distance normalized by country)",
                       dep.var.caption="$d\\_party$ normalized by country",
                       dep.var.labels = c("",""),
                       omit = c("miss_succ"),
                       covariate.labels = c("party distance (normalized)",
                                            "severity",
                                            "years since transition",
                                            "press freedom",
                                            "opposition status"),
                       column.labels = c("Model 1", "Model 2",
                                         "Model 3", "Model 4",
                                         "Model 5"),
                       model.numbers = FALSE)

remove(m_1, m_2, m_3, m_4, m_5,tab_models)
